<?php session_start();
include "statics.php";
if(isset($_POST['paging']))
{
    $arrConditions=array();
    if($_POST['filter_expertise'] != 0)
    {
        array_push($arrConditions,"area_of_expertise=");
        array_push($arrConditions,$_POST['filter_expertise']);
    }
    if($_POST['filter_expertise'] != 0 && $_POST['filter_place'] != "All")
    {
        array_push($arrConditions,"AND");
        array_push($arrConditions,"place=");
        array_push($arrConditions,$_POST['filter_place']);
    }
    else if($_POST['filter_expertise'] == 0 && $_POST['filter_place'] != "All")
    {
        array_push($arrConditions,"place=");
        array_push($arrConditions,$_POST['filter_place']);
    }

    $sJoinConditions="";
    if($_POST['alpha'] == "true")
    {
        $sJoinConditions="ORDER BY long_name ";
    }
    else if($_POST['date'] == "true")
    {
        $sJoinConditions="ORDER BY conference_date ";
    }
    if($_POST['paging'] != "All")
        $sJoinConditions.="LIMIT " . $_POST['limit'] . "," . $_POST['paging'];
    $rsConferences="";
    $rsCount="";
    if(count($arrConditions) > 0)
    {
        $rsCount=rsSelect("COUNT(conference_id)","conference",$arrConditions);
        $rsConferences=rsSelect("*","conference",$arrConditions,$sJoinConditions);
    }
    else
    {
        $rsCount=rsSelect("COUNT(conference_id)","conference");
        $rsConferences=rsSelect("*","conference",NULL,$sJoinConditions);
    }
    if(is_string($rsCount))
        echo $rsCount;
    else
    {
        if(is_string($rsConferences))
            echo $rsConferences;
        else
        {
            $row=mysql_fetch_assoc($rsCount);
            echo $row['COUNT(conference_id)'] . "%%EOR%%";
            while($row=mysql_fetch_assoc($rsConferences))
            {
                $sName=preg_replace("/%%EOR%%/","",$row['long_name']);
                $rsArea=rsSelect("area_of_expertise_text","area_of_expertise",array("area_of_expertise_id=",$row['area_of_expertise']));
                if(is_string($rsArea))
                    echo $rsArea;
                else
                {
                    $rsPlace=rsSelect("country_name","country",array("country_id=",$row['place']));
                    if(is_string($rsPlace))
                        echo $rsPlace;
                    else
                    {
                        $rowArea=mysql_fetch_assoc($rsArea);
                        $rowPlace=mysql_fetch_assoc($rsPlace);
                        echo "<tr class='conference_row'><td>";
                        if(bSubscribed($_SESSION['member_id'],$row['conference_id']))
                            echo "<input type='checkbox' class='conference_box' name='conferences[]' disabled='disabled' style='outline:2px solid red;' value='{$row['conference_id']}'/>";
                        else
                            echo "<input type='checkbox' class='conference_box' name='conferences[]' style='outline:2px solid green;' value='{$row['conference_id']}'/>";

                        echo " <a class='conference_link' href='conferencePage2.php?conference_id={$row['conference_id']}'>" . $sName . "</a></td>";
                        echo "<td>{$rowPlace['country_name']}</td><td>{$rowArea['area_of_expertise_text']}</td>";
                        echo "<td>" . $row['conference_date'] . "</td></tr>";
                    }
                }
            }
        }
    }
}
else if(isset($_POST['subscribe']))
{
    $arrIds=$_POST['conf_ids'];
    $len=count($arrIds);
    for($i=0; $i < $len; $i++)
    {
        $rsSubscribed=rsSelect("*","member_privileges",array("member_id=",$_SESSION['member_id'],"AND","conference_id=",$arrIds[$i]));
        if(is_string($rsSubscribed))
        {
            echo $rsSubscribed . "ERROR CODE: 0" . __LINE__;
            exit();
        }
        else
        {
            if(mysql_num_rows($rsSubscribed) > 0)
            {
                echo "ERROR CODE: 0" . __LINE__;
                exit();
            }
            if(iInsert("member_privileges",array("member_id"=>$_SESSION['member_id'],"privileges_id"=>4,"conference_id"=>$arrIds[$i])) != 1)
            {
                echo "0";
                exit();
            }
        }
    }
    echo "1";
}
?>
